package modele;

public class Swimmer extends Action {

	protected final SequentialScheduler process;
	protected final ResourcefulUser<Basket> basketUser;
	protected final ResourcefulUser<Cubicle> cubicleUser;
	protected final String nom;

	public Swimmer(String nom, ResourcePool<Basket> b, ResourcePool<Cubicle> c,
			int timeToUndress, int timeToSwim, int timeToDress) {
		process = new SequentialScheduler();
		this.basketUser = new ResourcefulUser<Basket>();
		this.cubicleUser = new ResourcefulUser<Cubicle>();
		this.nom = nom;

		process.addAction(new TakeResourceAction<Basket>(b, basketUser));

		process.addAction(new TakeResourceAction<Cubicle>(c, cubicleUser));
		process.addAction(new ForeseeableAction(timeToUndress));
		process.addAction(new FreeResourceAction<Cubicle>(c, cubicleUser));

		process.addAction(new ForeseeableAction(timeToSwim));

		process.addAction(new TakeResourceAction<Cubicle>(c, cubicleUser));
		process.addAction(new ForeseeableAction(timeToDress));
		process.addAction(new FreeResourceAction<Cubicle>(c, cubicleUser));

		process.addAction(new FreeResourceAction<Basket>(b, basketUser));

	}

	@Override
	public boolean isReady() {
		return (basketUser.getRessource() == null);
	}

	@Override
	public boolean isFinished() {
		return process.isFinished();
	}

	@Override
	public void reallyDoStep() {
		System.out.print("	" + nameTheStep(process.index) + " ");
		process.reallyDoStep();
	}
	
	public static String nameTheStep(int index){
		switch (index) {
		case 0:
			return "taking basket";
		case 1:
			return "taking cubicle";
		case 2:
			return "undressing";
		case 3:
			return "freeing cubicle";
		case 4:
			return "swimming";
		case 5:
			return "taking cubicle";
		case 6:
			return "dressing";
		case 7:
			return "freeing cubicle";
		case 8:
			return "freeing basket";
		default:
			return "doing action";
		}
	}

	public String toString() {
		return nom;
	}

}
